package com.lh.music.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lh.music.R;
import com.lh.music.activity.SearchActivity;
import com.lh.music.adapter.VPFAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {
    private View view;
    private RadioGroup rg;
    private DiscoverFragment discoverFragment;
    private MusicFragment musicFragment;
    private FriendFragment friendFragment;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private VPFAdapter pagerAdapter;
    private OnMainFragmentClickListener onMainFragmentClickListener;
    public MainFragment() {
        // Required empty public constructor
    }

    public OnMainFragmentClickListener getOnMainFragmentClickListener() {
        return onMainFragmentClickListener;
    }

    public void setOnMainFragmentClickListener(OnMainFragmentClickListener onMainFragmentClickListener) {
        this.onMainFragmentClickListener = onMainFragmentClickListener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view==null){
            view=inflater.inflate(R.layout.fragment_main, container, false);
            initView();
            initFragment();
            pagerAdapter=new VPFAdapter(getChildFragmentManager(),fragmentList);
            viewPager.setAdapter(pagerAdapter);
            //viewpager和radiogroup的联动
            ((RadioButton)rg.getChildAt(0)).setChecked(true);
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId){
                        case R.id.one:
                            viewPager.setCurrentItem(0);
                            break;
                        case R.id.two:
                            viewPager.setCurrentItem(1);
                            break;
                        case R.id.three:
                            viewPager.setCurrentItem(2);
                            break;
                    }
                }
            });
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    ((RadioButton)rg.getChildAt(position)).setChecked(true);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
        return view;
    }
    private void initFragment() {
        fragmentList=new ArrayList<>();
        discoverFragment=new DiscoverFragment();
        musicFragment=new MusicFragment();
        friendFragment=new FriendFragment();
        fragmentList.add(discoverFragment);
        fragmentList.add(musicFragment);
        fragmentList.add(friendFragment);
    }

    private void initView() {
        rg= (RadioGroup) view.findViewById(R.id.rg);
        viewPager= (ViewPager)view.findViewById(R.id.viewPager);

        //点击按钮弹出drawerlayout
        view.findViewById(R.id.show_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onMainFragmentClickListener!=null){
                    onMainFragmentClickListener.showMenuClick();
                }
            }
        });
        //跳到搜索页面
        view.findViewById(R.id.search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),SearchActivity.class);
                startActivity(intent);
            }
        });
    }
    public interface  OnMainFragmentClickListener{
        void showMenuClick();
    }
}
